<?php
require_once('wpfunctions.php');
header("Content-type: application/json");
$request = json_decode(file_get_contents('php://input'));
if(!ip_isLoggedIn()) {
  return '{"error":"Please log in first."}';
}
if(!$request->path) {
  return '{"error":"No filename given"}';
} else if(!$request->data) {
  return '{"error":"Cannot save an empty file"}';
} else if(preg_match('/\.\./','/'.$request->path)) {
  return '{"error":"Cannot use .. in the filepath"}';
}
$path = ip_createFile('liturgy-library/'.$request->path);
$result = file_put_contents($path,json_encode($request->data));
if($result === false) {
  echo '{"error":"error writing \''.$path.'\'"}';
} else {
  echo "\"$path\"";
}
?>